package com.sskj.xyft.mine;


import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.base.data.User;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.MoneyValueFilter;
import com.sskj.base.utils.NumberUtil;
import com.sskj.base.utils.TipDialog;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xyft.MVPActivity;
import com.sskj.xyft.R;
import com.sskj.xyft.api.SystemInfo;

import butterknife.BindView;

/**
 * 出金
 *
 * @author Hey
 * Create at  2019/03/27
 */
public class CashOutActivity extends MVPActivity<CashOutPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.availableText)
    TextView availableText;
    @BindView(R.id.bank_spinner)
    TextView bankSpinner;
    @BindView(R.id.edt_bank)
    TextView edtBank;
    @BindView(R.id.sxfee)
    TextView sxFee;

    @BindView(R.id.real_money)
    TextView realMoney;

    @BindView(R.id.edt_price)
    EditText edtPrice;
    @BindView(R.id.totalPriceText)
    TextView totalPriceText;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private double usdRate;
    private double maxCashout;

    private boolean accountLoad, rateLoad;
    private TipDialog tipDialog;
    private double sxFeeMoney; // 手续费率
    private String sxFeeMoneyBig;// 手续费  超过5000之后，固定金额手续费

    @Override
    public int getLayoutId() {
        return R.layout.activity_cash_out;
    }

    @Override
    public CashOutPresenter getPresenter() {
        return new CashOutPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(v -> finish());

        edtPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    String editS = s.toString();
                    if (TextUtils.isEmpty(editS)) {
                        realMoney.setText("实到金额(¥):0");
                        return;
                    }
                    if (Double.valueOf(getText(availableText)) < Double.valueOf(editS)) {
                        edtPrice.setText(getText(availableText));
                        editS = getText(availableText);
                    }
                    if (Double.valueOf(editS) <= 5000) {
                        sxFee.setText("手续费:" + sxFeeMoney + "%");
                        realMoney.setText("实到金额(¥):" + NumberUtil.keepMax2(Double.valueOf(editS) - Double.valueOf(editS) * sxFeeMoney / 100));
                    } else {
                        sxFee.setText("手续费:" + sxFeeMoneyBig);
                        realMoney.setText("实到金额" + NumberUtil.keepMax2(Double.valueOf(editS) - Double.valueOf(sxFeeMoneyBig)));
                    }


                } catch (Exception e) {
                    realMoney.setText("实到金额(¥):0");

                }
            }
        });

        edtPrice.setFilters(new InputFilter[]{new MoneyValueFilter(2)});
        ClickUtil.click(submit, view -> {

            tipDialog = new TipDialog(this)
                    .setTitle("提示")
                    .setContent("请确认出金")
                    .setListener(dialog -> {
                        if (isEmpty(edtPrice)) {
                            ToastUtils.show("请输入出金金额");
                            return;
                        }
                        if (Double.parseDouble(getText(edtPrice)) <= 0) {
                            ToastUtils.show("出金金额必须大于0");
                            return;
                        }
                        mPresenter.cashOut(getText(edtPrice));
                    });
            tipDialog.show();

//
//            if (Double.parseDouble(getText(totalPriceText)) > maxCashout) {
//                ToastUtils.show("出金金额最大为$" + maxCashout);
//                return;
//                }


        });

    }

    @Override
    public void initData() {
        initRefreshLayout(smartRefreshLayout, false);
        mPresenter.getSystemInfo();
        mPresenter.getAccountDetail(account, true);
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getAccountDetail(account, false);
            mPresenter.getSystemInfo();
        });

    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, CashOutActivity.class);
        context.startActivity(intent);
    }

    public void setSysInfo(SystemInfo data) {
        if (data != null) {
            sxFee.setText("手续费:" + data.getGold_yield_fee() + "%");
            sxFeeMoney = Double.valueOf(data.getGold_yield_fee());
            sxFeeMoneyBig = data.getGold_fixed_fee();
//            rateLoad = true;
//            usdRate = data.getOutRate();
//            maxCashout = data.getMaxOutCharge();
        }
    }

    public void onPaySuccess() {
        ToastUtils.show("您的出金申请正在审核中，请耐心等待");
        tipDialog.dismiss();
        finish();

//        edtPrice.getText().clear();
//        mPresenter.getAccountDetail(account, true);
    }

    public void setAccountInfo(User data) {
        accountLoad = true;
        setText(availableText, data.getWallone());
        setText(bankSpinner, data.getCardtype());
        setText(edtBank, data.getCardaccount());
    }
}
